﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Directory.Web
{
	public partial class Admin_UserView : System.Web.UI.Page
	{
		private int pageIndex;
		private int pageSize = WebSettings.GetInstance().CategoryViewPageSizeForAdmin;
		private int totalRecords;
		private string pageKey = "page";
		private string roleKey = "roleindex";

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!int.TryParse(Request.QueryString[pageKey], out pageIndex))
			{
				pageIndex = 0;
			}

			int roleIndex;
			if (!IsPostBack)
			{
				if (!int.TryParse(Request.QueryString[roleKey], out roleIndex))
				{
					roleIndex = 0;
				}
				ddlRoles.SelectedIndex = roleIndex;
				int roleRecords;
				ddlRoles.DataSource = RoleDataProxy.GetAllRoles(0, 999, out roleRecords);
				ddlRoles.DataBind();

				rptUsers.DataSource = RoleDataProxy.GetUsersInRole(ddlRoles.SelectedValue, pageIndex, pageSize, out totalRecords);
				rptUsers.DataBind();

				PageBreak.PageIndex = pageIndex;
				PageBreak.PageSize = pageSize;
				PageBreak.PageKey = pageKey;
				PageBreak.TotalRecords = totalRecords;
				PageBreak.NavigatorUrl = Request.Url.AbsolutePath + "?" + roleKey + "=" + ddlRoles.SelectedIndex.ToString() + "&";
			}
		}

		protected void ddlRoles_SelectedIndexChanged(object sender, EventArgs e)
		{
			Response.Redirect("UserView.aspx?" + roleKey + "=" + ddlRoles.SelectedIndex,true);
		}
	}
}
